package com.mydroidchat.gui;

import com.mydroidchat.data.ChatMessage;

import javax.swing.*;
import java.awt.*;

public class ChatArea extends JList {
    public ChatArea() {
        setCellRenderer(new DefaultListCellRenderer() {
            public Component getListCellRendererComponent(JList list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
                if (value == null) {
                    return super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
                }
                return new JEditorPane("text/html", format((ChatMessage) value));
            }
        });
    }

    private String format(ChatMessage value) {
        StringBuilder builder = new StringBuilder();
        builder.append("<html><body><font size=8pt><b>");
        builder.append(value.getUser().getNick());
        builder.append(": </b>");
        builder.append(value.getText());
        builder.append("</font><hr>");
        builder.append("</body></html>");
        return builder.toString();
    }
}
